{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-12T08:06:54.432903Z",
     "start_time": "2025-10-12T08:06:51.106098Z"
    }
   },
   "source": [
    "#导入数据\n",
    "#csv\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = pd.read_csv(\"data/employees.csv\")\n",
    "print(data)\n",
    "\n",
    "data.tail(5).to_csv('data/newdata.csv')"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     employee_id first_name last_name     email  phone_number      job_id  \\\n",
      "0            100     Steven      King     SKING  515.123.4567     AD_PRES   \n",
      "1            101      N_ann   Kochhar  NKOCHHAR  515.123.4568       AD_VP   \n",
      "2            102        Lex   De Haan   LDEHAAN  515.123.4569       AD_VP   \n",
      "3            103  Alexander    Hunold   AHUNOLD  590.423.4567     IT_PROG   \n",
      "4            104      Bruce     Ernst    BERNST  590.423.4568     IT_PROG   \n",
      "..           ...        ...       ...       ...           ...         ...   \n",
      "102          202        Pat       Fay      PFAY  603.123.6666      MK_REP   \n",
      "103          203      Susan    Mavris   SMAVRIS  515.123.7777      HR_REP   \n",
      "104          204    Hermann      Baer     HBAER  515.123.8888      PR_REP   \n",
      "105          205    Shelley   Higgins  SHIGGINS  515.123.8080      AC_MGR   \n",
      "106          206    William     Gietz    WGIETZ  515.123.8181  AC_ACCOUNT   \n",
      "\n",
      "      salary  commission_pct  manager_id  department_id  \n",
      "0    24000.0             NaN         NaN           90.0  \n",
      "1    17000.0             NaN       100.0           90.0  \n",
      "2    17000.0             NaN       100.0           90.0  \n",
      "3     9000.0             NaN       102.0           60.0  \n",
      "4     6000.0             NaN       103.0           60.0  \n",
      "..       ...             ...         ...            ...  \n",
      "102   6000.0             NaN       201.0           20.0  \n",
      "103   6500.0             NaN       101.0           40.0  \n",
      "104  10000.0             NaN       101.0           70.0  \n",
      "105  12000.0             NaN       101.0          110.0  \n",
      "106   8300.0             NaN       205.0          110.0  \n",
      "\n",
      "[107 rows x 10 columns]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T10:54:06.910998Z",
     "start_time": "2025-10-09T10:54:06.897929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#json\n",
    "data = pd.read_json(\"data/data1.json\")\n",
    "print(data)\n",
    "data.to_json(\"data/newdata.json\")\n",
    "import json\n",
    "with open(\"data/test.json\",encoding=\"utf-8\") as f:\n",
    "    data = json.load(f)\n",
    "print(data)\n",
    "df = pd.DataFrame(data['users'])\n",
    "print( df)"
   ],
   "id": "aa107dd6796bd2a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id name  age\n",
      "0   1   张三   25\n",
      "1   2   李四   30\n",
      "2   3   王五   28\n",
      "{'users': [{'id': 1, 'name': '张三', 'age': 28, 'email': 'zhangsan@example.com', 'is_active': True, 'join_date': '2022-03-15'}, {'id': 2, 'name': '李四', 'age': 35, 'email': 'lisi@example.com', 'is_active': False, 'join_date': '2021-11-02'}, {'id': 3, 'name': '王五', 'age': 24, 'email': 'wangwu@example.com', 'is_active': True, 'join_date': '2023-01-20'}]}\n",
      "   id name  age                 email  is_active   join_date\n",
      "0   1   张三   28  zhangsan@example.com       True  2022-03-15\n",
      "1   2   李四   35      lisi@example.com      False  2021-11-02\n",
      "2   3   王五   24    wangwu@example.com       True  2023-01-20\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T11:33:06.803872Z",
     "start_time": "2025-10-09T11:33:06.767912Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#缺失值的处理\n",
    "df = pd.read_csv(\"data/weather_withna.csv\")\n",
    "print(df.isna().sum())\n",
    "\n",
    "#删除缺失值\n",
    "print(df.dropna())#删除行\n",
    "print(df.dropna(axis=1))#删除列\n",
    "print(df.dropna(how=\"all\"))#删除都是NA的行\n",
    "print(df.dropna(thresh=3))#删除少于3个非NA的行\n",
    "print(df.dropna(subset=[\"wind\",\"temp_max\"]))#删除wind,temp_max任意列有NA的行\n",
    "\n",
    "#填充缺失值\n",
    "print(df.fillna(0))#数据中全部的nan填充0\n",
    "print(df.fillna({\"wind\":0,\"temp_max\":0}))#填充指定的列\n",
    "print(df.fillna({\"temp_min\":df[\"temp_min\"].mean()}))#填充列的均值\n",
    "print(df.fillna(df[[\"temp_min\"]].mean()))#填充列的均值\n",
    "print(df.ffill())#填充前一行数据\n",
    "print(df[\"temp_min\"].bfill())#填充后一行数据\n",
    "\n",
    "#去除重复行\n",
    "print(df.drop_duplicates())#去除重复行\n",
    "print(df.drop_duplicates(subset=[\"temp_min\"]))#去某一列除重复行\n",
    "\n",
    "#改变数据格式\n",
    "print(df.dtypes)\n",
    "df[\"weather\"] = df[\"weather\"].astype(\"category\")#改变数据格式\n",
    "print(df.dtypes)"
   ],
   "id": "c8102794e275c9e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date               0\n",
      "precipitation    303\n",
      "temp_max         303\n",
      "temp_min         303\n",
      "wind             303\n",
      "weather          303\n",
      "dtype: int64\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1153  2015-02-27           18.3      10.0       6.7   4.0     rain\n",
      "1154  2015-02-28            0.0      12.2       3.3   5.1      sun\n",
      "1155  2015-03-01            0.0      11.1       1.1   2.2      sun\n",
      "1156  2015-03-02            0.0      11.1       4.4   4.8      sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1158 rows x 6 columns]\n",
      "            date\n",
      "0     2012-01-01\n",
      "1     2012-01-02\n",
      "2     2012-01-03\n",
      "3     2012-01-04\n",
      "4     2012-01-05\n",
      "...          ...\n",
      "1456  2015-12-27\n",
      "1457  2015-12-28\n",
      "1458  2015-12-29\n",
      "1459  2015-12-30\n",
      "1460  2015-12-31\n",
      "\n",
      "[1461 rows x 1 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            NaN       NaN       NaN   NaN      NaN\n",
      "1457  2015-12-28            NaN       NaN       NaN   NaN      NaN\n",
      "1458  2015-12-29            NaN       NaN       NaN   NaN      NaN\n",
      "1459  2015-12-30            NaN       NaN       NaN   NaN      NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1153  2015-02-27           18.3      10.0       6.7   4.0     rain\n",
      "1154  2015-02-28            0.0      12.2       3.3   5.1      sun\n",
      "1155  2015-03-01            0.0      11.1       1.1   2.2      sun\n",
      "1156  2015-03-02            0.0      11.1       4.4   4.8      sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1158 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1153  2015-02-27           18.3      10.0       6.7   4.0     rain\n",
      "1154  2015-02-28            0.0      12.2       3.3   5.1      sun\n",
      "1155  2015-03-01            0.0      11.1       1.1   2.2      sun\n",
      "1156  2015-03-02            0.0      11.1       4.4   4.8      sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1158 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            0.0       0.0       0.0   0.0        0\n",
      "1457  2015-12-28            0.0       0.0       0.0   0.0        0\n",
      "1458  2015-12-29            0.0       0.0       0.0   0.0        0\n",
      "1459  2015-12-30            0.0       0.0       0.0   0.0        0\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            NaN       0.0       NaN   0.0      NaN\n",
      "1457  2015-12-28            NaN       0.0       NaN   0.0      NaN\n",
      "1458  2015-12-29            NaN       0.0       NaN   0.0      NaN\n",
      "1459  2015-12-30            NaN       0.0       NaN   0.0      NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8  5.000000   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6  2.800000   4.5     rain\n",
      "2     2012-01-03            0.8      11.7  7.200000   2.3     rain\n",
      "3     2012-01-04           20.3      12.2  5.600000   4.7     rain\n",
      "4     2012-01-05            1.3       8.9  2.800000   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            NaN       NaN  7.877202   NaN      NaN\n",
      "1457  2015-12-28            NaN       NaN  7.877202   NaN      NaN\n",
      "1458  2015-12-29            NaN       NaN  7.877202   NaN      NaN\n",
      "1459  2015-12-30            NaN       NaN  7.877202   NaN      NaN\n",
      "1460  2015-12-31           20.6      12.2  5.000000   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8  5.000000   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6  2.800000   4.5     rain\n",
      "2     2012-01-03            0.8      11.7  7.200000   2.3     rain\n",
      "3     2012-01-04           20.3      12.2  5.600000   4.7     rain\n",
      "4     2012-01-05            1.3       8.9  2.800000   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            NaN       NaN  7.877202   NaN      NaN\n",
      "1457  2015-12-28            NaN       NaN  7.877202   NaN      NaN\n",
      "1458  2015-12-29            NaN       NaN  7.877202   NaN      NaN\n",
      "1459  2015-12-30            NaN       NaN  7.877202   NaN      NaN\n",
      "1460  2015-12-31           20.6      12.2  5.000000   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            0.0      11.1       4.4   4.8      sun\n",
      "1457  2015-12-28            0.0      11.1       4.4   4.8      sun\n",
      "1458  2015-12-29            0.0      11.1       4.4   4.8      sun\n",
      "1459  2015-12-30            0.0      11.1       4.4   4.8      sun\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "0       5.0\n",
      "1       2.8\n",
      "2       7.2\n",
      "3       5.6\n",
      "4       2.8\n",
      "       ... \n",
      "1456    5.0\n",
      "1457    5.0\n",
      "1458    5.0\n",
      "1459    5.0\n",
      "1460    5.0\n",
      "Name: temp_min, Length: 1461, dtype: float64\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4     2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "...          ...            ...       ...       ...   ...      ...\n",
      "1456  2015-12-27            NaN       NaN       NaN   NaN      NaN\n",
      "1457  2015-12-28            NaN       NaN       NaN   NaN      NaN\n",
      "1458  2015-12-29            NaN       NaN       NaN   NaN      NaN\n",
      "1459  2015-12-30            NaN       NaN       NaN   NaN      NaN\n",
      "1460  2015-12-31           20.6      12.2       5.0   3.8     rain\n",
      "\n",
      "[1461 rows x 6 columns]\n",
      "            date  precipitation  temp_max  temp_min  wind  weather\n",
      "0     2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1     2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2     2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3     2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "5     2012-01-06            2.5       4.4       2.2   2.2     rain\n",
      "9     2012-01-10            1.0       6.1       0.6   3.4     rain\n",
      "10    2012-01-11            0.0       6.1      -1.1   5.1      sun\n",
      "11    2012-01-12            0.0       6.1      -1.7   1.9      sun\n",
      "12    2012-01-13            0.0       5.0      -2.8   1.3      sun\n",
      "14    2012-01-15            5.3       1.1      -3.3   3.2     snow\n",
      "16    2012-01-17            8.1       3.3       0.0   5.6     snow\n",
      "20    2012-01-21            3.0       8.3       3.3   8.2     rain\n",
      "22    2012-01-23            0.0       8.3       1.1   3.6     rain\n",
      "24    2012-01-25            8.1       8.9       4.4   5.4     rain\n",
      "26    2012-01-27            0.0       6.7      -2.2   1.4  drizzle\n",
      "28    2012-01-29           27.7       9.4       3.9   4.5     rain\n",
      "29    2012-01-30            3.6       8.3       6.1   5.1     rain\n",
      "32    2012-02-02            0.0       8.3       1.7   2.6      sun\n",
      "37    2012-02-07            0.3      15.6       7.8   5.3     rain\n",
      "40    2012-02-10            2.5      12.8       6.7   3.0     rain\n",
      "58    2012-02-28            3.6       6.7      -0.6   4.2     snow\n",
      "100   2012-04-10            0.0      17.8       8.9   3.2     rain\n",
      "112   2012-04-22            0.0      23.3       8.3   2.6     rain\n",
      "114   2012-04-24            4.3      13.9      10.0   2.8     rain\n",
      "128   2012-05-08            0.0      18.3       9.4   3.0      sun\n",
      "134   2012-05-14            0.0      26.7      12.8   3.8      sun\n",
      "140   2012-05-20            6.4      14.4      11.7   1.3     rain\n",
      "150   2012-05-30            0.3      18.9      11.1   1.5     rain\n",
      "151   2012-05-31            3.8      17.8      12.2   2.7     rain\n",
      "153   2012-06-02            0.3      18.9      10.6   3.7     rain\n",
      "167   2012-06-16            0.0      21.1      15.0   4.1     rain\n",
      "181   2012-06-30            3.0      20.0      13.3   2.4     rain\n",
      "189   2012-07-08            0.0      28.3      14.4   2.8     rain\n",
      "194   2012-07-13            0.5      23.3      13.9   2.2     rain\n",
      "216   2012-08-04            0.0      33.9      16.7   3.7      sun\n",
      "217   2012-08-05            0.0      33.9      17.8   1.9      sun\n",
      "218   2012-08-06            0.0      28.3      15.6   2.5     rain\n",
      "228   2012-08-16            0.0      34.4      18.3   2.8      sun\n",
      "229   2012-08-17            0.0      32.8      16.1   1.8      sun\n",
      "377   2013-01-12            0.0       2.8      -3.9   2.0      sun\n",
      "378   2013-01-13            0.0       2.2      -4.4   1.5      sun\n",
      "546   2013-06-30            0.0      33.9      17.2   2.5      sun\n",
      "690   2013-11-21            0.0       7.8      -0.5   3.6      sun\n",
      "703   2013-12-04            0.0       4.4      -2.1   1.6      sun\n",
      "704   2013-12-05            0.0       1.1      -4.9   2.6      sun\n",
      "705   2013-12-06            0.0       1.1      -4.3   4.7      sun\n",
      "706   2013-12-07            0.0       0.0      -7.1   3.1      sun\n",
      "707   2013-12-08            0.0       2.2      -6.6   2.2      sun\n",
      "710   2013-12-11            0.0       5.0      -1.6   0.8      sun\n",
      "766   2014-02-05            0.0      -0.5      -5.5   6.6      sun\n",
      "767   2014-02-06            0.0      -1.6      -6.0   4.5      sun\n",
      "1065  2014-12-01            0.0       4.4      -3.2   2.2      sun\n",
      "1095  2014-12-31            0.0       3.3      -2.7   3.0      sun\n",
      "1157  2015-03-03            NaN       NaN       NaN   NaN      NaN\n",
      "date              object\n",
      "precipitation    float64\n",
      "temp_max         float64\n",
      "temp_min         float64\n",
      "wind             float64\n",
      "weather           object\n",
      "dtype: object\n",
      "date               object\n",
      "precipitation     float64\n",
      "temp_max          float64\n",
      "temp_min          float64\n",
      "wind              float64\n",
      "weather          category\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T10:17:32.279769Z",
     "start_time": "2025-10-10T10:17:32.239653Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据转换\n",
    "#格式转换\n",
    "df= pd.read_csv(\"data/weather.csv\").tail(6)\n",
    "print(df)\n",
    "#长表格转换成宽表格\n",
    "print(pd.melt(df,id_vars=[\"date\",\"precipitation\",\"wind\",\"weather\"],var_name=\"temp_type\",value_name=\"temp\"))\n",
    "#宽表格转换成长表格\n",
    "print(pd.pivot(df,index=[\"date\",\"precipitation\",\"temp_min\",\"temp_max\"],columns=\"weather\",values=\"wind\"))\n",
    "\n",
    "#列分割\n",
    "df[[\"year\",\"month\",\"day\"]]=df[\"date\"].str.split(\"-\",expand=True)\n",
    "print(df)"
   ],
   "id": "18f21af5a9d6f7eb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            date  precipitation  temp_max  temp_min  wind weather\n",
      "1455  2015-12-26            0.0       4.4       0.0   2.5     sun\n",
      "1456  2015-12-27            8.6       4.4       1.7   2.9    rain\n",
      "1457  2015-12-28            1.5       5.0       1.7   1.3    rain\n",
      "1458  2015-12-29            0.0       7.2       0.6   2.6     fog\n",
      "1459  2015-12-30            0.0       5.6      -1.0   3.4     sun\n",
      "1460  2015-12-31            0.0       5.6      -2.1   3.5     sun\n",
      "          date  precipitation  wind weather temp_type  temp\n",
      "0   2015-12-26            0.0   2.5     sun  temp_max   4.4\n",
      "1   2015-12-27            8.6   2.9    rain  temp_max   4.4\n",
      "2   2015-12-28            1.5   1.3    rain  temp_max   5.0\n",
      "3   2015-12-29            0.0   2.6     fog  temp_max   7.2\n",
      "4   2015-12-30            0.0   3.4     sun  temp_max   5.6\n",
      "5   2015-12-31            0.0   3.5     sun  temp_max   5.6\n",
      "6   2015-12-26            0.0   2.5     sun  temp_min   0.0\n",
      "7   2015-12-27            8.6   2.9    rain  temp_min   1.7\n",
      "8   2015-12-28            1.5   1.3    rain  temp_min   1.7\n",
      "9   2015-12-29            0.0   2.6     fog  temp_min   0.6\n",
      "10  2015-12-30            0.0   3.4     sun  temp_min  -1.0\n",
      "11  2015-12-31            0.0   3.5     sun  temp_min  -2.1\n",
      "weather                                     fog  rain  sun\n",
      "date       precipitation temp_min temp_max                \n",
      "2015-12-26 0.0            0.0     4.4       NaN   NaN  2.5\n",
      "2015-12-27 8.6            1.7     4.4       NaN   2.9  NaN\n",
      "2015-12-28 1.5            1.7     5.0       NaN   1.3  NaN\n",
      "2015-12-29 0.0            0.6     7.2       2.6   NaN  NaN\n",
      "2015-12-30 0.0           -1.0     5.6       NaN   NaN  3.4\n",
      "2015-12-31 0.0           -2.1     5.6       NaN   NaN  3.5\n",
      "            date  precipitation  temp_max  temp_min  wind weather  year month  \\\n",
      "1455  2015-12-26            0.0       4.4       0.0   2.5     sun  2015    12   \n",
      "1456  2015-12-27            8.6       4.4       1.7   2.9    rain  2015    12   \n",
      "1457  2015-12-28            1.5       5.0       1.7   1.3    rain  2015    12   \n",
      "1458  2015-12-29            0.0       7.2       0.6   2.6     fog  2015    12   \n",
      "1459  2015-12-30            0.0       5.6      -1.0   3.4     sun  2015    12   \n",
      "1460  2015-12-31            0.0       5.6      -2.1   3.5     sun  2015    12   \n",
      "\n",
      "     day  \n",
      "1455  26  \n",
      "1456  27  \n",
      "1457  28  \n",
      "1458  29  \n",
      "1459  30  \n",
      "1460  31  \n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T08:06:58.919125Z",
     "start_time": "2025-10-12T08:06:58.860408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据分箱\n",
    "df= pd.read_csv(\"data/weather.csv\").head(10)\n",
    "print(df)\n",
    "df = df[[\"date\",'wind']]\n",
    "df['date'] = df[\"date\"].str.split(\"-\").str[-1]\n",
    "df.set_index(\"date\",inplace=True)#设置索引\n",
    "#df.reset_index(inplace=True)#恢复索引\n",
    "print(df)\n",
    "df = pd.cut(df[\"wind\"],3)#按照数值,平分为3个分箱\n",
    "print(df)\n",
    "print(df.value_counts())\n",
    "df= pd.read_csv(\"data/weather.csv\").head(10)\n",
    "print(df)\n",
    "df = df[[\"date\",'wind']]\n",
    "df['date'] = df[\"date\"].str.split(\"-\").str[-1]\n",
    "df.set_index(\"date\",inplace=True)\n",
    "df = pd.qcut(df[\"wind\"],3,labels=[\"low\",\"medium\",\"high\"])#按照数频,平分为3个分箱\n",
    "print(df.value_counts())\n",
    "\n",
    "df= pd.read_csv(\"data/weather.csv\").head(10)\n",
    "print(df)\n",
    "df = df[[\"date\",'wind']]\n",
    "df.rename(columns={\"wind\":\"wind_level\"},inplace=True)#重命名列\n",
    "df.rename(index={0:1},inplace=True)#重命名行\n",
    "print(df)"
   ],
   "id": "b60e36a6490f1032",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         date  precipitation  temp_max  temp_min  wind  weather\n",
      "0  2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1  2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2  2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3  2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4  2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "5  2012-01-06            2.5       4.4       2.2   2.2     rain\n",
      "6  2012-01-07            0.0       7.2       2.8   2.3     rain\n",
      "7  2012-01-08            0.0      10.0       2.8   2.0      sun\n",
      "8  2012-01-09            4.3       9.4       5.0   3.4     rain\n",
      "9  2012-01-10            1.0       6.1       0.6   3.4     rain\n",
      "      wind\n",
      "date      \n",
      "01     4.7\n",
      "02     4.5\n",
      "03     2.3\n",
      "04     4.7\n",
      "05     6.1\n",
      "06     2.2\n",
      "07     2.3\n",
      "08     2.0\n",
      "09     3.4\n",
      "10     3.4\n",
      "date\n",
      "01    (3.367, 4.733]\n",
      "02    (3.367, 4.733]\n",
      "03    (1.996, 3.367]\n",
      "04    (3.367, 4.733]\n",
      "05      (4.733, 6.1]\n",
      "06    (1.996, 3.367]\n",
      "07    (1.996, 3.367]\n",
      "08    (1.996, 3.367]\n",
      "09    (3.367, 4.733]\n",
      "10    (3.367, 4.733]\n",
      "Name: wind, dtype: category\n",
      "Categories (3, interval[float64, right]): [(1.996, 3.367] < (3.367, 4.733] < (4.733, 6.1]]\n",
      "wind\n",
      "(3.367, 4.733]    5\n",
      "(1.996, 3.367]    4\n",
      "(4.733, 6.1]      1\n",
      "Name: count, dtype: int64\n",
      "         date  precipitation  temp_max  temp_min  wind  weather\n",
      "0  2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1  2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2  2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3  2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4  2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "5  2012-01-06            2.5       4.4       2.2   2.2     rain\n",
      "6  2012-01-07            0.0       7.2       2.8   2.3     rain\n",
      "7  2012-01-08            0.0      10.0       2.8   2.0      sun\n",
      "8  2012-01-09            4.3       9.4       5.0   3.4     rain\n",
      "9  2012-01-10            1.0       6.1       0.6   3.4     rain\n",
      "wind\n",
      "low       4\n",
      "high      4\n",
      "medium    2\n",
      "Name: count, dtype: int64\n",
      "         date  precipitation  temp_max  temp_min  wind  weather\n",
      "0  2012-01-01            0.0      12.8       5.0   4.7  drizzle\n",
      "1  2012-01-02           10.9      10.6       2.8   4.5     rain\n",
      "2  2012-01-03            0.8      11.7       7.2   2.3     rain\n",
      "3  2012-01-04           20.3      12.2       5.6   4.7     rain\n",
      "4  2012-01-05            1.3       8.9       2.8   6.1     rain\n",
      "5  2012-01-06            2.5       4.4       2.2   2.2     rain\n",
      "6  2012-01-07            0.0       7.2       2.8   2.3     rain\n",
      "7  2012-01-08            0.0      10.0       2.8   2.0      sun\n",
      "8  2012-01-09            4.3       9.4       5.0   3.4     rain\n",
      "9  2012-01-10            1.0       6.1       0.6   3.4     rain\n",
      "         date  wind_level\n",
      "1  2012-01-01         4.7\n",
      "1  2012-01-02         4.5\n",
      "2  2012-01-03         2.3\n",
      "3  2012-01-04         4.7\n",
      "4  2012-01-05         6.1\n",
      "5  2012-01-06         2.2\n",
      "6  2012-01-07         2.3\n",
      "7  2012-01-08         2.0\n",
      "8  2012-01-09         3.4\n",
      "9  2012-01-10         3.4\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T08:50:57.683639Z",
     "start_time": "2025-10-12T08:50:57.668608Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#pandas的时间类型\n",
    "date = pd.Timestamp(\"2025-5-26 12:30:30\")\n",
    "print(date.year)\n",
    "print(date.month)\n",
    "print(date.day)\n",
    "print(date.hour)\n",
    "print(date.minute)\n",
    "print(date.second)\n",
    "print(date.day_name())#该日期是星期几\n",
    "#时间的转换\n",
    "print(date.to_period(\"M\"))#将日期转为相应的时间段\n",
    "print(date.to_period(\"D\"))#将日期转为相应的时间段\n",
    "#字符串转时间\n",
    "a = \"2025-5-26 12:30:30\"\n",
    "date = pd.to_datetime(a)\n",
    "print(date)\n",
    "#csv文件时间的转换\n",
    "# df = pd.read_csv(\"data/weather.csv\").head(15)\n",
    "# print(df)\n",
    "# df['date'] = pd.to_datetime(df['date'])\n",
    "# print(df[\"date\"].dt.day_name())\n",
    "df = pd.read_csv(\"data/weather.csv\" ,parse_dates=[\"date\"]).head(15)\n",
    "print(df[\"date\"].dt.day_name())\n",
    "#时间差\n",
    "date1 = pd.Timestamp(\"2025-5-26\")\n",
    "date2 = pd.Timestamp(\"2025-5-27\")\n",
    "print(date2-date1)\n",
    "df[\"date\"] = df['date'] - df['date'][0]\n",
    "df.set_index(\"date\",inplace=True)\n",
    "print(df.loc[pd.Timedelta(days=10):])\n",
    "print(df.loc[df.index>pd.Timedelta(days=10)])#索引查询格式"
   ],
   "id": "7eb55bba914f7766",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025\n",
      "5\n",
      "26\n",
      "12\n",
      "30\n",
      "30\n",
      "Monday\n",
      "2025-05\n",
      "2025-05-26\n",
      "2025-05-26 12:30:30\n",
      "0        Sunday\n",
      "1        Monday\n",
      "2       Tuesday\n",
      "3     Wednesday\n",
      "4      Thursday\n",
      "5        Friday\n",
      "6      Saturday\n",
      "7        Sunday\n",
      "8        Monday\n",
      "9       Tuesday\n",
      "10    Wednesday\n",
      "11     Thursday\n",
      "12       Friday\n",
      "13     Saturday\n",
      "14       Sunday\n",
      "Name: date, dtype: object\n",
      "1 days 00:00:00\n",
      "         precipitation  temp_max  temp_min  wind weather\n",
      "date                                                    \n",
      "10 days            0.0       6.1      -1.1   5.1     sun\n",
      "11 days            0.0       6.1      -1.7   1.9     sun\n",
      "12 days            0.0       5.0      -2.8   1.3     sun\n",
      "13 days            4.1       4.4       0.6   5.3    snow\n",
      "14 days            5.3       1.1      -3.3   3.2    snow\n",
      "         precipitation  temp_max  temp_min  wind weather\n",
      "date                                                    \n",
      "11 days            0.0       6.1      -1.7   1.9     sun\n",
      "12 days            0.0       5.0      -2.8   1.3     sun\n",
      "13 days            4.1       4.4       0.6   5.3    snow\n",
      "14 days            5.3       1.1      -3.3   3.2    snow\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T09:28:36.916215Z",
     "start_time": "2025-10-12T09:28:36.903552Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#分组聚合\n",
    "df = pd.read_csv(\"data/employees.csv\" )\n",
    "print(df)\n",
    "df.dropna(subset=['salary','job_id'], inplace=True)\n",
    "print(df.groupby('employee_id').get_group(100))#获取分组\n",
    "print(df.groupby('employee_id')['salary'].mean())\n",
    "# df =df.groupby(['employee_id','job_id'])[['salary']].mean()\n",
    "# print(type(df))\n",
    "# df = df.groupby(['employee_id','job_id']).agg({'salary':['mean','max','min']})\n",
    "# print(df)\n",
    "df = df.groupby(['employee_id','job_id'])['salary'].mean()\n",
    "print(df)\n",
    "# df.reset_index(inplace=True)\n",
    "# print(df)"
   ],
   "id": "ab154c1138bd3ae7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     employee_id first_name last_name     email  phone_number      job_id  \\\n",
      "0            100     Steven      King     SKING  515.123.4567     AD_PRES   \n",
      "1            101      N_ann   Kochhar  NKOCHHAR  515.123.4568       AD_VP   \n",
      "2            102        Lex   De Haan   LDEHAAN  515.123.4569       AD_VP   \n",
      "3            103  Alexander    Hunold   AHUNOLD  590.423.4567     IT_PROG   \n",
      "4            104      Bruce     Ernst    BERNST  590.423.4568     IT_PROG   \n",
      "..           ...        ...       ...       ...           ...         ...   \n",
      "102          202        Pat       Fay      PFAY  603.123.6666      MK_REP   \n",
      "103          203      Susan    Mavris   SMAVRIS  515.123.7777      HR_REP   \n",
      "104          204    Hermann      Baer     HBAER  515.123.8888      PR_REP   \n",
      "105          205    Shelley   Higgins  SHIGGINS  515.123.8080      AC_MGR   \n",
      "106          206    William     Gietz    WGIETZ  515.123.8181  AC_ACCOUNT   \n",
      "\n",
      "      salary  commission_pct  manager_id  department_id  \n",
      "0    24000.0             NaN         NaN           90.0  \n",
      "1    17000.0             NaN       100.0           90.0  \n",
      "2    17000.0             NaN       100.0           90.0  \n",
      "3     9000.0             NaN       102.0           60.0  \n",
      "4     6000.0             NaN       103.0           60.0  \n",
      "..       ...             ...         ...            ...  \n",
      "102   6000.0             NaN       201.0           20.0  \n",
      "103   6500.0             NaN       101.0           40.0  \n",
      "104  10000.0             NaN       101.0           70.0  \n",
      "105  12000.0             NaN       101.0          110.0  \n",
      "106   8300.0             NaN       205.0          110.0  \n",
      "\n",
      "[107 rows x 10 columns]\n",
      "   employee_id first_name last_name  email  phone_number   job_id   salary  \\\n",
      "0          100     Steven      King  SKING  515.123.4567  AD_PRES  24000.0   \n",
      "\n",
      "   commission_pct  manager_id  department_id  \n",
      "0             NaN         NaN           90.0  \n",
      "employee_id\n",
      "100    24000.0\n",
      "101    17000.0\n",
      "102    17000.0\n",
      "103     9000.0\n",
      "104     6000.0\n",
      "        ...   \n",
      "202     6000.0\n",
      "203     6500.0\n",
      "204    10000.0\n",
      "205    12000.0\n",
      "206     8300.0\n",
      "Name: salary, Length: 107, dtype: float64\n",
      "employee_id  job_id    \n",
      "100          AD_PRES       24000.0\n",
      "101          AD_VP         17000.0\n",
      "102          AD_VP         17000.0\n",
      "103          IT_PROG        9000.0\n",
      "104          IT_PROG        6000.0\n",
      "                            ...   \n",
      "202          MK_REP         6000.0\n",
      "203          HR_REP         6500.0\n",
      "204          PR_REP        10000.0\n",
      "205          AC_MGR        12000.0\n",
      "206          AC_ACCOUNT     8300.0\n",
      "Name: salary, Length: 107, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T09:50:05.094216Z",
     "start_time": "2025-10-12T09:50:05.074306Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#测试一\n",
    "df = pd.read_csv(\"data/penguins.csv\")\n",
    "print(df)\n",
    "df.dropna(inplace= True)\n",
    "label = ['low','medium','high']\n",
    "df['body_mass_g'] = pd.cut(df['body_mass_g'],3,labels=label)\n",
    "print(df.value_counts('body_mass_g'))\n",
    "df = df.groupby(['sex','island'])[[\"bill_length_mm\"]].agg({\n",
    "    'bill_length_mm':['mean','max','min']\n",
    "})\n",
    "print(df)"
   ],
   "id": "dba26e1375cf6518",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    species     island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
      "0    Adelie  Torgersen            39.1           18.7              181.0   \n",
      "1    Adelie  Torgersen            39.5           17.4              186.0   \n",
      "2    Adelie  Torgersen            40.3           18.0              195.0   \n",
      "3    Adelie  Torgersen             NaN            NaN                NaN   \n",
      "4    Adelie  Torgersen            36.7           19.3              193.0   \n",
      "..      ...        ...             ...            ...                ...   \n",
      "339  Gentoo     Biscoe             NaN            NaN                NaN   \n",
      "340  Gentoo     Biscoe            46.8           14.3              215.0   \n",
      "341  Gentoo     Biscoe            50.4           15.7              222.0   \n",
      "342  Gentoo     Biscoe            45.2           14.8              212.0   \n",
      "343  Gentoo     Biscoe            49.9           16.1              213.0   \n",
      "\n",
      "     body_mass_g     sex  \n",
      "0         3750.0    Male  \n",
      "1         3800.0  Female  \n",
      "2         3250.0  Female  \n",
      "3            NaN     NaN  \n",
      "4         3450.0  Female  \n",
      "..           ...     ...  \n",
      "339          NaN     NaN  \n",
      "340       4850.0  Female  \n",
      "341       5750.0    Male  \n",
      "342       5200.0  Female  \n",
      "343       5400.0    Male  \n",
      "\n",
      "[344 rows x 7 columns]\n",
      "body_mass_g\n",
      "low       150\n",
      "medium    128\n",
      "high       55\n",
      "Name: count, dtype: int64\n",
      "                 bill_length_mm            \n",
      "                           mean   max   min\n",
      "sex    island                              \n",
      "Female Biscoe         43.307500  50.5  34.5\n",
      "       Dream          42.296721  58.0  32.1\n",
      "       Torgersen      37.554167  41.1  33.5\n",
      "Male   Biscoe         47.119277  59.6  37.6\n",
      "       Dream          46.116129  55.8  36.3\n",
      "       Torgersen      40.586957  46.0  34.6\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T10:09:58.842562Z",
     "start_time": "2025-10-12T10:09:58.819915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#测试二\n",
    "df = pd.read_csv(\"data/sleep.csv\")\n",
    "print(df.isna().sum())\n",
    "df.drop(columns=['sleep_disorder'],inplace=True)\n",
    "print(df)\n",
    "df[['height_blood','lower_blood']]=df['blood_pressure'].str.split('/',expand=True)\n",
    "df.drop(columns=['blood_pressure'],inplace=True)\n",
    "print(df)\n",
    "df['occupation'] = df['occupation'].astype('category')\n",
    "df['bmi_category'] =df['bmi_category'].astype('category')\n",
    "labels = ['low','medium','high']\n",
    "df['age'] = pd.cut(df['age'],3,labels=labels)\n",
    "print(df['age'].value_counts())\n",
    "df['height_blood'] = df['height_blood'].astype('int')\n",
    "df['lower_blood'] = df['lower_blood'].astype('int')\n",
    "df = df.groupby(['occupation','bmi_category']).agg({\n",
    "    'height_blood':['mean','max','min'],\n",
    "    'lower_blood':['mean','max','min']\n",
    "}).round(2)\n",
    "print(df)"
   ],
   "id": "2c30eb97629ef5b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "person_id                    0\n",
      "gender                       0\n",
      "age                          0\n",
      "occupation                   0\n",
      "sleep_duration               0\n",
      "sleep_quality                0\n",
      "physical_activity_level      0\n",
      "stress_level                 0\n",
      "bmi_category                 0\n",
      "blood_pressure               0\n",
      "heart_rate                   0\n",
      "daily_steps                  0\n",
      "sleep_disorder             290\n",
      "dtype: int64\n",
      "     person_id  gender  age     occupation  sleep_duration  sleep_quality  \\\n",
      "0            1    Male   29   Manual Labor             7.4            7.0   \n",
      "1            2  Female   43        Retired             4.2            4.9   \n",
      "2            3    Male   44        Retired             6.1            6.0   \n",
      "3            4    Male   29  Office Worker             8.3           10.0   \n",
      "4            5    Male   67        Retired             9.1            9.5   \n",
      "..         ...     ...  ...            ...             ...            ...   \n",
      "395        396  Female   36        Student             4.5            7.9   \n",
      "396        397  Female   45   Manual Labor             6.0            6.1   \n",
      "397        398  Female   30        Student             5.3            6.5   \n",
      "398        399  Female   41        Retired            11.0            9.1   \n",
      "399        400    Male   37        Retired             5.8            7.0   \n",
      "\n",
      "     physical_activity_level  stress_level bmi_category blood_pressure  \\\n",
      "0                         41             7        Obese         124/70   \n",
      "1                         41             5        Obese         131/86   \n",
      "2                        107             4  Underweight         122/70   \n",
      "3                         20            10        Obese         124/72   \n",
      "4                         19             4   Overweight         133/78   \n",
      "..                       ...           ...          ...            ...   \n",
      "395                       73             7       Normal         118/66   \n",
      "396                       72             8        Obese         132/80   \n",
      "397                       58            10        Obese         125/76   \n",
      "398                       73             9        Obese         130/75   \n",
      "399                       41             6       Normal         118/70   \n",
      "\n",
      "     heart_rate  daily_steps  \n",
      "0            91         8539  \n",
      "1            81        18754  \n",
      "2            81         2857  \n",
      "3            55         6886  \n",
      "4            97        14945  \n",
      "..          ...          ...  \n",
      "395          64        14497  \n",
      "396          65        12848  \n",
      "397          66        15255  \n",
      "398          75         6567  \n",
      "399          51        18079  \n",
      "\n",
      "[400 rows x 12 columns]\n",
      "     person_id  gender  age     occupation  sleep_duration  sleep_quality  \\\n",
      "0            1    Male   29   Manual Labor             7.4            7.0   \n",
      "1            2  Female   43        Retired             4.2            4.9   \n",
      "2            3    Male   44        Retired             6.1            6.0   \n",
      "3            4    Male   29  Office Worker             8.3           10.0   \n",
      "4            5    Male   67        Retired             9.1            9.5   \n",
      "..         ...     ...  ...            ...             ...            ...   \n",
      "395        396  Female   36        Student             4.5            7.9   \n",
      "396        397  Female   45   Manual Labor             6.0            6.1   \n",
      "397        398  Female   30        Student             5.3            6.5   \n",
      "398        399  Female   41        Retired            11.0            9.1   \n",
      "399        400    Male   37        Retired             5.8            7.0   \n",
      "\n",
      "     physical_activity_level  stress_level bmi_category  heart_rate  \\\n",
      "0                         41             7        Obese          91   \n",
      "1                         41             5        Obese          81   \n",
      "2                        107             4  Underweight          81   \n",
      "3                         20            10        Obese          55   \n",
      "4                         19             4   Overweight          97   \n",
      "..                       ...           ...          ...         ...   \n",
      "395                       73             7       Normal          64   \n",
      "396                       72             8        Obese          65   \n",
      "397                       58            10        Obese          66   \n",
      "398                       73             9        Obese          75   \n",
      "399                       41             6       Normal          51   \n",
      "\n",
      "     daily_steps height_blood lower_blood  \n",
      "0           8539          124          70  \n",
      "1          18754          131          86  \n",
      "2           2857          122          70  \n",
      "3           6886          124          72  \n",
      "4          14945          133          78  \n",
      "..           ...          ...         ...  \n",
      "395        14497          118          66  \n",
      "396        12848          132          80  \n",
      "397        15255          125          76  \n",
      "398         6567          130          75  \n",
      "399        18079          118          70  \n",
      "\n",
      "[400 rows x 13 columns]\n",
      "age\n",
      "low       232\n",
      "medium    151\n",
      "high       17\n",
      "Name: count, dtype: int64\n",
      "                           height_blood           lower_blood        \n",
      "                                   mean  max  min        mean max min\n",
      "occupation    bmi_category                                           \n",
      "Manual Labor  Normal             118.91  136  109       71.55  94  60\n",
      "              Obese              129.67  138  119       79.19  93  63\n",
      "              Overweight         119.67  136  109       70.75  87  60\n",
      "              Underweight        118.38  129  109       69.62  91  60\n",
      "Office Worker Normal             119.86  135  109       69.82  86  60\n",
      "              Obese              131.19  145  119       80.46  94  63\n",
      "              Overweight         119.96  136  109       70.62  82  60\n",
      "              Underweight        118.24  128  109       69.44  81  60\n",
      "Retired       Normal             122.43  135  109       72.43  85  60\n",
      "              Obese              129.00  145  120       79.88  95  63\n",
      "              Overweight         121.27  140  109       72.00  86  60\n",
      "              Underweight        118.83  133  109       70.58  89  60\n",
      "Student       Normal             120.00  145  109       71.08  96  60\n",
      "              Obese              129.15  142  119       81.04  96  63\n",
      "              Overweight         120.42  143  109       70.64  95  60\n",
      "              Underweight        119.00  133  109       70.46  91  60\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\吴思洋\\AppData\\Local\\Temp\\ipykernel_40880\\791312496.py:16: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  df = df.groupby(['occupation','bmi_category']).agg({\n"
     ]
    }
   ],
   "execution_count": 100
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
